import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../../base/EnumIndex";
import { Tools } from "../../common/Tools";
import { AudioManager1 } from "../../manager/AudioManager1";
import { EventManager } from "../../manager/EventManager";
import { UIManager } from "../../manager/UIManager";
import LevelBase from "../../LevelBase";
import LvItem from "../../LvItem";
import LevelPre_15 from "./LevelPre_15";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_15_194 extends cc.Component {

    level15: LevelPre_15 = null
    audioCount: number = 0;

    /** 第几层 */
    louceng = 1;

    start() {
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);

    }

    onDisable() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);
    }


    gN(NodeName: string) {
        if (!this.level15) {
            this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
        }
        if (!this.level15.getNodeForName(NodeName).getComponent(LvItem)) {
            this.level15.getNodeForName(NodeName).addComponent(LvItem);
        }
        return this.level15.getNodeForName(NodeName);
    }
    gNa(NodeName: string, active: boolean) {
        this.level15.getNodeForName(NodeName).active = active;
    }

    async finish(finishStr: any, lvItem: LvItem) {
        if (typeof finishStr !== "string") { return }

        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);

        if (finishStr === "reviveGame") {
            console.log("复活");
            AudioManager1.playBG("恐怖关卡背景音乐", 1, BUNDLE_TYPE_ENUM.LEVEL)
            this.gN("duqi").opacity = 0;
            this.gNa("一楼伪人", true);
            this.gNa("二楼伪人", true);
            this.gNa("三楼伪人", true);
            this.gNa("四楼伪人", true);
            this.gNa("长官", false);
            this.gNa("屋子变暗", false);
            this.gNa("xiaohei", false);
            this.gNa("突脸", false);
            switch (this.louceng) {
                case 1:
                    this.gNa("一楼门", true);
                    this.gNa("下滑耳朵", true);
                    this.gNa("双击眼睛", true);
                    this.level15.removeStates(["1", "2"]);
                    this.gN("jinglinger").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);
                    this.scheduleOnce(() => {
                        this.gN("jinglinger").getComponent(sp.Skeleton).findBone("精灵耳朵").active = true;
                        this.gN("jinglinger").getComponent(sp.Skeleton).findBone("美瞳").active = true;
                    });
                    break;
                case 2:
                    this.gNa("二楼门", true);
                    this.level15.removeStates(["3", "4", "5", "丝巾"]);
                    this.gNa("拖动丝巾", true);
                    this.gNa("丝巾", false);
                    this.gNa("拖动眼镜", true);
                    this.gNa("拖动口罩", true);
                    this.gNa("下滑二楼抽屉", true);
                    this.gNa("刮胡刀", false);
                    this.gNa("抽屉打开", false);

                    this.gN("daye").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);

                    this.gN("daye").getComponent(sp.Skeleton).findBone("中毒口罩").active = true;
                    this.gN("daye").getComponent(sp.Skeleton).findBone("中毒络腮胡").active = true;
                    this.gN("daye").getComponent(sp.Skeleton).findBone("中毒老花镜").active = true;
                    this.gN("daye").getComponent(sp.Skeleton).findBone("中毒衬衣里的丝巾").active = true;
                    this.gN("daye").getComponent(sp.Skeleton).findBone("口罩").active = true;
                    this.gN("daye").getComponent(sp.Skeleton).findBone("络腮胡").active = true;
                    this.gN("daye").getComponent(sp.Skeleton).findBone("老花镜").active = true;
                    this.gN("daye").getComponent(sp.Skeleton).findBone("衬衣里的丝巾").active = true;
                    break;
                case 3:
                    this.gNa("三楼门", true);
                    this.gNa("拖动刘海", true);
                    this.gNa("拖动头套", true);
                    this.gNa("拖动创可贴", true);
                    this.gNa("下滑三楼抽屉", true);
                    this.gNa("抽屉打开3楼", false);
                    this.gNa("刀", false);
                    this.level15.removeStates(["6", "7", "8", "9"]);

                    this.gN("jiejie").getComponent(sp.Skeleton).setAnimation(0, "dou2", true);
                    this.gN("jiejie").getComponent(sp.Skeleton).findBone("防水肿头套").active = true;
                    this.gN("jiejie").getComponent(sp.Skeleton).findBone("刘海").active = true;
                    this.gN("jiejie").getComponent(sp.Skeleton).findBone("缝合线").active = true;
                    this.gN("jiejie").getComponent(sp.Skeleton).findBone("中毒防水肿头套").active = true;
                    this.gN("jiejie").getComponent(sp.Skeleton).findBone("中毒刘海").active = true;
                    this.gN("jiejie").getComponent(sp.Skeleton).findBone("中毒缝合线").active = true;
                    this.scheduleOnce(() => {
                        this.gN("jiejie").getComponent(sp.Skeleton).setAttachment("缝合线", "创可贴");
                        this.gN("jiejie").getComponent(sp.Skeleton).setAttachment("中毒缝合线", "中毒创可贴");
                    })
                    break;
                case 4:
                    this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);
                    this.gN("nng").setPosition(0, -400);
                    this.gNa("四楼门", true);
                    this.gNa("浴室门", true);
                    this.gNa("毛巾", true);
                    this.gNa("拖动绷带", true);
                    this.gNa("喷头", true);
                    this.gNa("装红牛奶瓶", false);
                    this.gNa("空牛奶瓶", true);
                    this.gNa("nng", true);
                    this.gNa("右滑浴室门", true);
                    this.gNa("浴缸的血", false);

                    this.gNa("假牛奶工头2", false);
                    this.gNa("假牛奶工头1", true);
                    this.gN("nng").getComponent(sp.Skeleton).findBone("握瓶子").active = false;
                    this.gN("nng").getComponent(sp.Skeleton).findBone("匕首").active = true;
                    this.gN("nng").getComponent(sp.Skeleton).findBone("绷带").active = true;
                    this.level15.removeStates(["10", "11", "12", "毛巾", "绷带"]);
                    break;
            }
            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("fuhuo").interTween);

        }
        if (finishStr === "ifPen") {
            if (this.louceng == 4 && this.level15.isHaveStates(["12"])) {
                this.level15.runNodeTween(this.node, this.level15.getInterTwForName("直接抓捕").interTween);
            } else {
                this.level15.runNodeTween(this.node, this.level15.getInterTwForName("喷毒气").interTween);
            }
        }
        if (finishStr === "updateGame") {
            this.gNa("按钮", false);
            switch (this.louceng) {
                case 1:
                    this.gNa("一楼门", false);
                    AudioManager1.playAudio("女中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.gN("jinglinger").getComponent(sp.Skeleton).setAnimation(0, "zhongdu", true);
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);

                    break;
                case 2:
                    this.gNa("二楼门", false);
                    AudioManager1.playAudio("男中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.gN("daye").getComponent(sp.Skeleton).setAnimation(0, "dou", true);
                    if (this.level15.isHaveStates(["3", "4", "5"])) {

                        this.level15.addHaveStates(["22"])
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("下一层").interTween);
                        this.louceng++;
                        this.level15.removeStates(["丝巾"]);
                    } else {
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);
                    }
                    break;
                case 3:
                    this.gNa("三楼门", false);
                    AudioManager1.playAudio("女中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.gN("jiejie").getComponent(sp.Skeleton).setAnimation(0, "dou", true);
                    if (this.level15.isHaveStates(["6", "7", "8", "9"])) {

                        this.scheduleOnce(() => {
                            this.gN("jiejie").getComponent(sp.Skeleton).setAttachment("中毒缝合线", "中毒脸上嘴");
                        })

                        this.level15.addHaveStates(["23"])
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("下一层").interTween);
                        this.louceng++;
                    } else {
                        this.scheduleOnce(() => {
                            if (this.level15.isHaveNoStates(["6"])) {
                                this.gN("jiejie").getComponent(sp.Skeleton).setAttachment("中毒缝合线", "中毒创可贴");
                            } else if (this.level15.isHaveNoStates(["9"])) {
                                this.gN("jiejie").getComponent(sp.Skeleton).setAttachment("中毒缝合线", "中毒缝合线");
                            } else {
                                this.gN("jiejie").getComponent(sp.Skeleton).setAttachment("中毒缝合线", "中毒脸上嘴");
                            }
                        })
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);
                    }
                    break;
                case 4:
                    this.gNa("四楼门", false);
                    if (this.level15.isHaveStates(["10", "11", "12"])) {
                        this.level15.addHaveStates(["24"])
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("winGameAni").interTween);
                    } else {

                        AudioManager1.playAudio("男中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)

                        this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "dou2", true);
                        if (this.level15.isHaveStates(["毛巾"])) {
                            this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "dou3", true);
                        }
                        if (this.level15.isHaveStates(["12"])) {
                            this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "dou", true);
                        }
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni2").interTween);

                    }
                    break;
            }
        }
        if (finishStr === "updateGame2") {
            this.gNa("按钮", false);
            this.gN("jiancha").scaleX = -1;
            switch (this.louceng) {
                case 1:
                    if (this.level15.isHaveStates(["1", "2"])) {
                        this.level15.addHaveStates(["21"])
                        let sayInfo = { "node": "行动队SayNode", "str": "该居民嫌疑已排除！", "audio": { "resUrl": "该居民嫌疑已排除！", "value": 1 } }
                        await this.level15.sayNodeQipaoTween(sayInfo);
                        this.scheduleOnce(() => {
                            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("下一层2").interTween);
                        }, 2)
                        this.louceng++;
                    } else {
                        let sayInfo = { "node": "行动队SayNode", "str": "该居民嫌疑已排除！", "audio": { "resUrl": "该居民嫌疑已排除！", "value": 1 } }
                        await this.level15.sayNodeQipaoTween(sayInfo);
                        this.scheduleOnce(() => {
                            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni4").interTween);
                        }, 2)
                    }
                    break;
                case 2:
                case 3:
                    let sayInfo1 = { "node": "行动队SayNode", "str": "该居民嫌疑已排除！", "audio": { "resUrl": "该居民嫌疑已排除！", "value": 1 } }
                    await this.level15.sayNodeQipaoTween(sayInfo1);
                    this.scheduleOnce(() => {
                        this.gNa("屋子变暗", true);
                        this.gNa("xiaohei", true);
                        this.gNa("一楼伪人", false);
                        this.gNa("二楼伪人", false);
                        this.gNa("三楼伪人", false);
                        cc.tween(this.gN("xiaohei")).set({ position: cc.v3(170, -550) }).delay(1).by(0.5, { x: -200 }).start();
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni1").interTween);
                    }, 2)
                    break;
                case 4:

                    let sayInfo2 = { "node": "行动队SayNode", "str": "该居民嫌疑已排除！", "audio": { "resUrl": "该居民嫌疑已排除！", "value": 1 } }
                    await this.level15.sayNodeQipaoTween(sayInfo2);
                    this.scheduleOnce(() => {
                        this.gNa("屋子变暗", true);
                        this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "chouchu", true);
                        this.gN("nng").getComponent(sp.Skeleton).findBone("握瓶子").active = true;
                        this.gN("nng").getComponent(sp.Skeleton).findBone("匕首").active = false;
                        cc.tween(this.gN("nng")).set({ position: cc.v3(0, -400) }).delay(1).by(0.5, { x: -260 }).call(() => {
                            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGameAni").interTween);
                        }).start();
                    }, 2)
                    break;
            }
        }
        if (finishStr === "突击检查") {

            let XingdongduiSayInfo1 = { "node": "行动队SayNode", "str": "不许动，突击检查！", "audio": { "resUrl": "不许动，突击检查！", "value": 1 } }
            let XingdongduiSayInfo2 = { "node": "行动队SayNode", "str": "不许动，把刀放下！", "audio": { "resUrl": "不许动，把刀放下！", "value": 1 } }

            let sayInfo1 = { "node": "一楼伪人SayNode", "str": "桥豆麻袋，我只是在玩COS啊！", "audio": { "resUrl": "桥豆麻袋，我只是在玩COS啊！", "value": 1 } }
            let XingdongduiTime = 0;
            let time = 0;
            switch (this.louceng) {
                case 1:
                    XingdongduiTime = await this.level15.sayNodeQipaoTween(XingdongduiSayInfo1);
                    this.gNa("一楼门", false);
                    break;
                case 2:
                    XingdongduiTime = await this.level15.sayNodeQipaoTween(XingdongduiSayInfo1);
                    this.gNa("二楼门", false);
                    break;
                case 3:
                    XingdongduiTime = await this.level15.sayNodeQipaoTween(XingdongduiSayInfo1);
                    this.gNa("三楼门", false);
                    break;
                case 4:
                    XingdongduiTime = await this.level15.sayNodeQipaoTween(XingdongduiSayInfo2);
                    this.gNa("四楼门", false);
                    break;
            }
            this.scheduleOnce(async () => {
                switch (this.louceng) {
                    case 1:
                        this.gN("jinglinger").getComponent(sp.Skeleton).setAnimation(0, "shuohua", true);
                        time = await this.level15.sayNodeQipaoTween(sayInfo1);
                        this.gNa("一楼门", false);
                        break;
                    case 2:
                        this.gNa("二楼门", false);
                        break;
                    case 3:
                        this.gNa("三楼门", false);
                        break;
                    case 4:
                        this.gNa("四楼门", false);
                        this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "diao", false);
                        AudioManager1.playAudio("匕首掉落", 1, BUNDLE_TYPE_ENUM.LEVEL)
                        time = 1;
                        break;
                }

                this.scheduleOnce(() => {
                    switch (this.louceng) {
                        case 1:
                            this.gN("jinglinger").getComponent(sp.Skeleton).setAnimation(0, "daiji", true);
                            break;
                        case 2:
                            break;
                        case 3:
                            break;
                        case 4:
                            this.gN("nng").getComponent(sp.Skeleton).setAnimation(0, "diao_daiji", true);
                            break;
                    }
                    this.gN("按钮").active = true;
                    this.gN("按钮").opacity = 0;
                    cc.tween(this.gN("按钮"))
                        .to(0.5, { opacity: 255 })
                        .call(() => {
                            UIManager.ins.enabledGameClick(true);
                        })
                        .start()
                }, time)
            }, XingdongduiTime)
        }
        if (finishStr === "startGame") {
            this.gN("nng").getComponent(sp.Skeleton).findBone("握瓶子").active = false;
        }
        if (finishStr.includes("提示字")) {
            this.typewriterEffect();
        }
    }


    typewriterEffect() {
        this.schedule(this.typeNextCharacter, 1 / this.typingSpeed);
    }

    typeNextCharacter() {
        if (this.typingFinish) {
            this.unschedule(this.typeNextCharacter);
            return;
        }

        // 添加下一个字符到文本内容
        this.textLabel.getComponent(cc.Label).string += this.content[this.currentIndex];

        // 更新当前索引
        this.currentIndex++;

        // 判断是否打字完成
        if (this.currentIndex >= this.content.length) {
            this.typingFinish = true;
        }
    }

    @property(cc.Node)
    textLabel: cc.Node = null;

    @property({ displayName: "打字速度" })
    typingSpeed: number = 3; // 打字速度，每秒多少个字符

    @property({ displayName: "文本内容" })
    content: string = ""; // 完整的文本内容

    private currentIndex: number = 0; // 当前打字到的索引
    private typingFinish: boolean = false; // 打字是否完成



}
